package utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期格式：yyyyMMddHHmmssSSS
 * 
 */
public class DateUtil {
	/**
	 * 获取时间 从Date到String
	 * 
	 * @param date
	 * @return yyyyMMddHHmmssSSS
	 */
	public static String get(Date date) {
		if (date == null) {
			return "00000000000000000";
		}
		String result = "";
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		int day = c.get(Calendar.DAY_OF_MONTH);
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);
		int second = c.get(Calendar.SECOND);
		int millisecond = c.get(Calendar.MILLISECOND);
		result += year;
		if (month < 10) {
			result += "0";
		}
		result += month;
		if (day < 10) {
			result += "0";
		}
		result += day;
		if (hour < 10) {
			result += "0";
		}
		result += hour;
		if (minute < 10) {
			result += "0";
		}
		result += minute;
		if (second < 10) {
			result += "0";
		}
		result += second;
		if (millisecond < 10) {
			result += "00";
		} else if (millisecond < 100) {
			result += "0";
		}
		result += millisecond;
		return result;
	}

	/**
	 * 获取时间 从String到Date
	 * 
	 * @param date
	 *            yyyyMMddHHmmssSSS
	 * @return
	 */
	public static Date get(String date) {
		String year = date.substring(0, 4);
		String month = date.substring(4, 6);
		String day = date.substring(6, 8);
		String hour = date.substring(8, 10);
		String minute = date.substring(10, 12);
		String second = date.substring(12, 14);
		String millisecond = date.substring(14, 17);
		Calendar c = Calendar.getInstance();
		try {
			c.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer
					.parseInt(day), Integer.parseInt(hour), Integer
					.parseInt(minute), Integer.parseInt(second));
			c.set(Calendar.MILLISECOND, Integer.parseInt(millisecond));
		} catch (Exception e) {
			c.set(0, 0, 0, 0, 0, 0);
			c.set(Calendar.MILLISECOND, 0);
		}
		return c.getTime();
	}

	/**
	 * 获取时间 从String到Date
	 * 
	 * @param date
	 * @param format
	 *            yyyyMMddHHmmssSSS
	 * @return
	 */
	public static Date get(String date, String format) {
		try {
			return new SimpleDateFormat(format).parse(date);
		} catch (ParseException e) {
		}
		return null;
	}

	/**
	 * 获取当前时间
	 * 
	 * @return yyyyMMddHHmmssSSS
	 */
	public static String get() {
		return get(new Date());
	}

	/**
	 * 格式化日期
	 * 
	 * @param date
	 *            yyyyMMddHHmmssSSS
	 * @param format
	 *            yyyyMMddHHmmssSSS
	 * @return
	 */
	public static String format(String date, String format) {
		return date.replaceAll(
				"(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{3})",
				format.replaceAll("yyyy", "\\$1").replaceAll("MM", "\\$2")
						.replaceAll("dd", "\\$3").replaceAll("HH", "\\$4")
						.replaceAll("mm", "\\$5").replaceAll("ss", "\\$6")
						.replaceAll("SSS", "\\$7"));
	}

	/**
	 * 格式化日期
	 * 
	 * @param date
	 * @param format
	 *            yyyyMMddHHmmssSSS
	 * @return
	 */
	public static String format(Date date, String format) {
		if (date == null) {
			date = get("00000000000000000");
		}
		return format(get(date), format);
	}

	/**
	 * 日期增加
	 * 
	 * @param date
	 *            yyyyMMddHHmmssSSS
	 * @param field
	 * @param value
	 * @return
	 */
	public static String dateAdd(String date, String field, int value) {
		Calendar c = Calendar.getInstance();
		c.setTime(get(date));
		int cField = 0;
		if (field.equals("yyyy")) {
			cField = Calendar.YEAR;
		} else if (field.equals("MM")) {
			cField = Calendar.MONTH;
		} else if (field.equals("dd")) {
			cField = Calendar.DAY_OF_MONTH;
		} else if (field.equals("HH")) {
			cField = Calendar.HOUR_OF_DAY;
		} else if (field.equals("mm")) {
			cField = Calendar.MINUTE;
		} else if (field.equals("ss")) {
			cField = Calendar.SECOND;
		} else if (field.equals("SSS")) {
			cField = Calendar.MILLISECOND;
		}
		c.add(cField, value);
		return get(c.getTime());
	}

	/**
	 * 日期相加
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static Date dateAdd(Date date1, Date date2) {
		if (date1 == null) {
			date1 = get("00000000000000000");
		}
		if (date2 == null) {
			date2 = get("00000000000000000");
		}
		Calendar c1 = Calendar.getInstance();
		c1.setTime(date1);
		Calendar c2 = Calendar.getInstance();
		c2.setTime(date2);
		c1.add(Calendar.YEAR, c2.get(Calendar.YEAR));
		c1.add(Calendar.MONTH, c2.get(Calendar.MONTH));
		c1.add(Calendar.DAY_OF_MONTH, c2.get(Calendar.DAY_OF_MONTH));
		c1.add(Calendar.HOUR_OF_DAY, c2.get(Calendar.HOUR_OF_DAY));
		c1.add(Calendar.MINUTE, c2.get(Calendar.MINUTE));
		c1.add(Calendar.SECOND, c2.get(Calendar.SECOND));
		c1.add(Calendar.MILLISECOND, c2.get(Calendar.MILLISECOND));
		return c1.getTime();
	}

	/**
	 * 日期差
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static long dateDiff(Date date1, Date date2) {
		if (date1 == null) {
			date1 = get("00000000000000000");
		}
		if (date2 == null) {
			date2 = get("00000000000000000");
		}
		return date1.getTime() - date2.getTime();
	}

	/**
	 * 日期差
	 * 
	 * @param field
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int dateDiff(String field, Date date1, Date date2) {
		if (date1 == null && date2 == null) {
			return 0;
		}
		if (date1 == null) {
			date1 = get("00000000000000000");
		}
		if (date2 == null) {
			date2 = get("00000000000000000");
		}
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(dateDiff(date1, date2));
		int cField = 0;
		if (field.equals("yyyy")) {
			cField = Calendar.YEAR;
		} else if (field.equals("MM")) {
			cField = Calendar.MONTH;
		} else if (field.equals("dd")) {
			cField = Calendar.DAY_OF_MONTH;
		} else if (field.equals("HH")) {
			cField = Calendar.HOUR_OF_DAY;
		} else if (field.equals("mm")) {
			cField = Calendar.MINUTE;
		} else if (field.equals("ss")) {
			cField = Calendar.SECOND;
		} else if (field.equals("SSS")) {
			cField = Calendar.MILLISECOND;
		}
		return c.get(cField);
	}

	/**
	 * 检查日期是否合法
	 * 
	 * @param date
	 * @return
	 */
	public static boolean isDate(String date) {
		if (date == null) {
			return false;
		}
		if (get(date, "yyyy-MM-dd") != null) {
			return true;
		}
		if (get(date, "yyyy.MM.dd") != null) {
			return true;
		}
		return false;
	}
}
